<?php
error_reporting(1);
require_once(APPPATH . '/controllers/test/Toast.php');

class user_account_tests extends Toast {

    var $CI;
    // var $logindata;
    var $logindata = array();
    var $deactivate = array();
    var $activate =array();
    function user_account_tests() {
        parent::Toast(__FILE__);
        $this->load->model('account_model');

// Load any models, libraries etc. you need here
    }

    /**
     * OPTIONAL; Anything in this function will be run before each test
     * Good for doing cleanup: resetting sessions, renewing objects, etc.
     */
    function _pre() {
////////////////////////////prepare array for login ////////////////////////////////////
        $this->logindata = array('deactivate' => array('session_key' =>random_string('alnum', 10),
                 'password' =>'1234',
                'emailAddress' => 'abhishek.sharad@rediffmail.com'
            ),
            'login' => array('session_key' => random_string('alnum', 10),
                'password' => '12345',
                'emailAddress' => 'er.rahul18mca@gmail.com'
            ),
            'login_sucessfully' => array('session_key' => random_string('alnum', 10),
                'password' =>'12345',
                'emailAddress' => 'abhishek.mm@gmail.com'
            )
        );
////////////////////////////prepae arry for logout//////////////////////////////////////////////

         $this->logout = array('logout_sucess' => array('sessionKey' => 'jQHnsc17QS'),
            'logout' => array('sessionKey' => '12345')
            
        );
        

////////////////////////////prepare array to insert new User ////////////////////////////////////
        $this->createUser = array('EmailIdAlreadyRegistered' => array('emailAddress' => 'showk@rediffmail.com',
                'password' =>md5('1234'),
                'firstName' => 'alok',
                'lastName' => 'dixit',
                'gTravelerName' => 'gTraveller',
              
            ),
            'userdetail' => array('address1' => 'allahabad',
                'address2' => 'alahapur',
                'country' => 'India',
                'city' => 'allahabd',
                'zip' => '123',
                'mobile' => '9935291242',
                'dateOfBirth' => '20-2-1988'
            ),
            'AccountCreatedSuccessfully' => array('emailAddress' => 'sharadit-mca.@gmail.com',
                'password' => md5('12345'),
                'firstName' => 'sharad',
                'lastName' => 'srivastava',
                'gTravelerName' => 'gTraveller',

            ),
            'userdetail1' => array('address1' => 'allahabad',
                
                'address2' => 'alahapur',
                'country' => 'India',
                'city' => 'allahabd',
                'zip' => '123',
                'mobile' => '9935291242',
                'dateOfBirth' => '20-2-1988'
            )
        );

        ////////////////////////////prepare array to update User ////////////////////////////////////
          $this->updateUser = array(
              'userFilterColumn'=>array('emailAddress' => 'aaa1111@gmail.com', 'password' => '1234'),
              'uuser'=>array('firstName' => 'baba',
                'lastName' => 'ji',
                'gTravelerName' => 'jai',
                'userGroup' => '1'),
            'uuserdetail' => array('address1' => 'kailash',
                'address2' => 'ka',
                'country' => 'india',
                'city' => 'allahabad',
                'zip' => '211004',
                'mobile' => '92129392939',
                'dateOfBirth' => '20-2-1988',
                'remarks' => 'subha'),
              
                
              'nuserFilterColumn'=>array('emailAddress' => 'aa@gmail.com', 'password' => '1234'),
              'nuuser'=>array('firstName' => 'baba',
                'lastName' => 'jee',
                'gTravelerName' => 'jai',
                'userGroup' => '1'),
            'nuserdetail' => array('address1' => 'kailash',
                'address2' => 'ka',
                'country' => 'india',
                'city' => 'allahabad',
                'zip' => '211004',
                'mobile' => '92129392939',
                'dateOfBirth' => '20-2-1988',
                'remarks' => 'subha')
                );
          





        

        ////////////////////////////prepare array to deactivate user ////////////////////////////////////
        $this->deactivate = array('deactivate' => array('emailAddress' => 'abhishek.sharad@rediffmail.com',
                'password' => '1234',
                'sessionKey' => 'r2oJxfXB9c'),
            'Invalid_Session_deactivate' => array('emailAddress' => 'er.rahul18mca@gmail.com',
                'password' => '123789',
                'sessionKey' => '1228987801'),
            'Login_firstToDeactivate_deactivate' => array('emailAddress' => 'er.rahul18mca@gmail.com',
                'password' =>'123789',
                'sessionKey' => '1228987801'),
            'Invalid_Email_deactivate' => array('emailAddress' => 'er.rahul18mca@gmail.com',
                'password' =>'123789', 
                'sessionKey' => '1228987801'),
        );


        $this->activate = array('invalidActivationCode' => array('emailAddress' => 'abhishek.sharad@rediffmail.com',
                'activationCode' => '0',
            ),
            'accountActivatedSuccessfully' => array('emailAddress' => 'er.rahul18mca@gmail.com',
                'activationCode' => '123',
            ),
            'invalidemailId' => array('emailAddress' => 'er.rahul18mca@gmail.com',
                'activationCode' => '123',
            ),
            'accountAlreadyActivated' => array('emailAddress' => 'er.rahul18mca@gmail.com',
                'activationCode' => '123',
            ),
        );
        
        $this->userdetail = array('validuser' => array('userID' => '2',
                'sessionKey' => '12312',
            ),
            'notvalid' => array('userID' => '1',
               'sessionKey' => '123',
            )
        );
    }

   
    function _post() {
        
    }

    /* TESTS BELOW */

    /////////////////////testing for user account////////////////////
    ////////////////////////testing for login. ////////////////////
    function test_login() {
        $logindata = $this->logindata;
        $result = "";
        $query1 = $this->account_model->validate_user($this->logindata['deactivate']);
        $result = $result . "<br/>Password:" . $this->logindata['deactivate']['password'] . "<br/>Email Address :" . $this->logindata['deactivate']['emailAddress'] . "<br/> Result :" . $this->login_result($query1) . "<br/><br/>";
        $query2 = $this->account_model->validate_user($this->logindata['login']);
        $result = $result . "<br/>Password:" . $this->logindata['login']['password'] . "<br/>Email Address :" . $this->logindata['login']['emailAddress'] . "<br/> Result :" . $this->login_result($query2) . "<br/><br/>";
        $query3 = $this->account_model->validate_user($this->logindata['login_sucessfully']);
        $result = $result . "<br/>Password:" . $this->logindata['login_sucessfully']['password'] . "<br/>Email Address :" . $this->logindata['login_sucessfully']['emailAddress'] . "<br/> Result :" . $this->login_result($query3) . "<br/><br/>";
             
        $this->message = $result;
    }

    
    
    function login_result($query) {
        
        
        if ($query == 0) {
            $this->_assert_equals($query, 0);
            return json_encode(array('status' => false, 'code' => 4, 'msg' => "Account is not activated yet"));
         
        } else if ($query == 1) {
            $this->_assert_equals($query, 1);
             return json_encode(array('status' => true, 'code' => 3, 'msg' => "Allready logged in"));
            
        } else if ($query == 2) {
            $this->_assert_equals($query, 2);
             return json_encode(array('status' => false, 'code' => 5, 'msg' => "Invalid UserName or Password"));
          
        } else if ($query == -1) {
            $this->json_encode(array('status' => false, 'code' => 1, 'msg' => "Log in unsuccessful"));
        } else {
             return json_encode(array('status' => false, 'code' => 1, 'msg' => "Log in unsuccess full"));
        }
        
    }
 //////////////////////////test logout function for sucessfull/////////////
    public function test_logout() {
     
        $logout = $this->logout;
        $result = "";
        
        $query1 = $this->account_model->logout_user($this->logout['logout_sucess']);
         
       
        $result = $result . "<br/>Password:" . $this->logout['logout_sucess']['sessionKey'] . "<br/> Result :" . $this->logout_result($query1) . "<br/><br/>";
     
           
        $query2 = $this->account_model->logout_user($this->logout['logout']);
        $result = $result . "<br/>Session Key:" . $this->logout['logout']['sessionKey']  . "<br/> result" . $this->logout_result($query2) . "<br/><br/>";
        
        $this->message = $result;

    }
function logout_result($query){
    if ($query == 1) {
            $this->_assert_equals($query, 1);
              return json_encode(array('status' => true, 'code' => 0, 'msg' => "logout successfully !"));
        } else  if ($query == 0)  {
             $this->_assert_equals($query, 0);
              return json_encode(array('status' => false, 'code' => 1, 'msg' => "Invalid session key"));
        }
}
//////////////////////test for create User/////////////////
    function test_create_user() {
        
        $result = "";
        $query1 = $this->account_model->create_user($this->createUser['EmailIdAlreadyRegistered'],$this->createUser['userdetail']);
        $result = $result . "For First Input:<br/> Email ID : " . $this->createUser['EmailIdAlreadyRegistered']['emailAddress'] . "<br/>Password:" . $this->createUser['EmailIdAlreadyRegistered']['password'] ."<br/>firstName:" . $this->createUser['EmailIdAlreadyRegistered']['firstName']."<br/>lastName:" . $this->createUser['EmailIdAlreadyRegistered']['lastName']."<br/>gTravelerName:" . $this->createUser['EmailIdAlreadyRegistered']['gTravelerName']."<br/>address1:" . $this->createUser['userdetail']['address1']."<br/>address2:" . $this->createUser['userdetail']['address2']."<br/>country:" . $this->createUser['userdetail']['country']."<br/>city:" . $this->createUser['userdetail']['city']."<br/>zip:" . $this->createUser['userdetail']['zip']."<br/>mobile:" . $this->createUser['userdetail']['mobile']."<br/>dateOfBirth:" . $this->createUser['userdetail']['dateOfBirth']. "<br/> Result :" . $this->create_user_result($query1) . "<br/><br/>";
        $query2 = $this->account_model->create_user($this->createUser['AccountCreatedSuccessfully'],$this->createUser['userdetail1']);
       $result = $result . "For Second Input:<br/> Email ID : " . $this->createUser['AccountCreatedSuccessfully']['emailAddress'] . "<br/>Password:" . $this->createUser['AccountCreatedSuccessfully']['password'] ."<br/>firstName:" . $this->createUser['AccountCreatedSuccessfully']['firstName']."<br/>lastName:" . $this->createUser['AccountCreatedSuccessfully']['lastName']."<br/>gTravelerName:" . $this->createUser['AccountCreatedSuccessfully']['gTravelerName']."<br/>address1:" . $this->createUser['userdetail1']['address1']."<br/>address2:" . $this->createUser['userdetail1']['address2']."<br/>country:" . $this->createUser['userdetail1']['country']."<br/>city:" . $this->createUser['userdetail1']['city']."<br/>zip:" . $this->createUser['userdetail1']['zip']."<br/>mobile:" . $this->createUser['userdetail1']['mobile']."<br/>dateOfBirth:" . $this->createUser['userdetail1']['dateOfBirth']. "<br/> Result :" . $this->create_user_result($query2) . "<br/><br/>";
        
        
        $this->message = $result;
    }

    function create_user_result($query) {
        if ($query == 0) {
            $this->_assert_equals($query, 0);
             return json_encode(array('status' => false, 'code' => 1, 'msg' => "This EmailId is already registered"));
           
        } else if ($query) {
            
              return json_encode(array('status' => true, 'code' => 0, 'msg' => "Activation code sent to user email"));
           
        } else {
             return json_encode(array('status' => false, 'code' => 1, 'msg' => "Server response error. Could not create."));
        }
    }
    
    
    //////////////////////test for update User/////////////////
    function test_update_user() {
       
        $result = "";
        
        $query1 = $this->account_model->update_user($this->updateUser['userFilterColumn'],$this->updateUser['uuser'],$this->updateUser['uuserdetail']);
        $result = $result . "For First Input:<br/> Email ID : " . $this->updateUser['userFilterColumn']['emailAddress'] . 
                "<br/>Password:" . $this->updateUser['userFilterColumn']['password'] .
                "<br/>firstName:" . $this->updateUser['uuser']['firstName'].
                "<br/>lastName:" . $this->updateUser['uuser']['lastName'].
                "<br/>gTravelerName:" . $this->updateUser['uuser']['gTravelerName'].
                 "<br/>userGroup:" . $this->updateUser['uuser']['userGroup'].
                "<br/>address1:" . $this->updateUser['uuserdetail']['address1'].
                "<br/>address2:" . $this->updateUser['uuserdetail']['address2'].
                "<br/>country:" . $this->updateUser['uuserdetail']['country'].
                "<br/>city:" . $this->updateUser['uuserdetail']['city'].
                "<br/>zip:" . $this->updateUser['uuserdetail']['zip'].
                "<br/>mobile:" . $this->updateUser['uuserdetail']['mobile'].
                "<br/>dateOfBirth:" . $this->updateUser['uuserdetail']['dateOfBirth']. 
                "<br/>remarks:" . $this->updateUser['uuserdetail']['remarks'].
                
                "<br/> Result :" . $this->update_user_result($query1) . "<br/><br/>";
        $query2 = $this->account_model->update_user($this->updateUser['nuserFilterColumn'],$this->updateUser['nuuser'],$this->updateUser['nuserdetail']);
        $result = $result . "For Second Input:<br/> Email ID : " . $this->updateUser['nuserFilterColumn']['emailAddress'] . 
                "<br/>Password:" . $this->updateUser['nuserFilterColumn']['password'] .
                "<br/>firstName:" . $this->updateUser['nuuser']['firstName'].
                "<br/>lastName:" . $this->updateUser['nuuser']['lastName'].
                "<br/>gTravelerName:" . $this->updateUser['nuuser']['gTravelerName'].
                "<br/>userGroup:" . $this->updateUser['nuuser']['userGroup'].
                "<br/>address1:" . $this->updateUser['nuserdetail']['address1'].
                "<br/>address2:" . $this->updateUser['nuserdetail']['address2'].
                "<br/>country:" . $this->updateUser['nuserdetail']['country'].
                "<br/>city:" . $this->updateUser['nuserdetail']['city'].
                "<br/>zip:" . $this->updateUser['nuserdetail']['zip'].
                "<br/>mobile:" . $this->updateUser['nuserdetail']['mobile'].
                "<br/>dateOfBirth:" . $this->updateUser['nuserdetail']['dateOfBirth'].
                "<br/>remarks:" . $this->updateUser['nuserdetail']['remarks'].
         "<br/> Result :" . $this->update_user_result($query2) . "<br/><br/>";
        $this->message = $result;
    }

    
    
     function update_user_result($query) {
      if($query){
       if ( $this->_assert_not_empty($query))
            return json_encode(array('status' => true, 'code' => 0, 'msg' => "Updated successfully"));
        
      }
         else {
              if ( $this->_assert_empty($query))
            return json_encode(array('status' => false, 'code' => 1, 'msg' => "could not update"));  
         }
        }
     
    
    

   
   
   

    ///////////////////////////////////////testing for deactivate account//////////////////

    public function test_deactivate() {

        $deactivate = $this->deactivate;

        $result = "";
        $query1 = $this->account_model->deactivate_user($this->deactivate['deactivate']);
        $result = $result . "For First Input:<br/> Email Address : " . $this->deactivate['deactivate']['emailAddress'] . "<br/>Password:" . $this->deactivate['deactivate']['password'] . "<br/>Session Key :" . $this->deactivate['deactivate']['sessionKey'] . "<br/> Result :" . $this->deactivate_result($query1) . "<br/><br/>";
        $query2 = $this->account_model->deactivate_user($this->deactivate['Invalid_Session_deactivate']);
        $result = $result . "For Second Input:<br/> Email Address : " . $this->deactivate['Invalid_Session_deactivate']['emailAddress'] . "<br/>Password:" . $this->deactivate['Invalid_Session_deactivate']['password'] . "<br/>Session Key :" . $this->deactivate['Invalid_Session_deactivate']['sessionKey'] . "<br/> Result :" . $this->deactivate_result($query2) . "<br/><br/>";

        $query3 = $this->account_model->deactivate_user($this->deactivate['Login_firstToDeactivate_deactivate']);
        $result = $result . "For Third Input:<br/> Email Address : " . $this->deactivate['Login_firstToDeactivate_deactivate']['emailAddress'] . "<br/>Password:" . $this->deactivate['Login_firstToDeactivate_deactivate']['password'] . "<br/>Session Key :" . $this->deactivate['Login_firstToDeactivate_deactivate']['sessionKey'] . "<br/> Result :" . $this->deactivate_result($query3) . "<br/><br/>";
        $query4 = $this->account_model->deactivate_user($this->deactivate['Invalid_Email_deactivate']);
        $result = $result . "For Fourth Input:<br/> Email Address : " . $this->deactivate['Invalid_Email_deactivate']['emailAddress'] . "<br/>Password:" . $this->deactivate['Invalid_Email_deactivate']['password'] . "<br/>Session Key :" . $this->deactivate['Invalid_Email_deactivate']['sessionKey'] . "<br/> Result :" . $this->deactivate_result($query4) . "<br/><br/>";
        $this->message = $result;
    }

    function deactivate_result($query) {
      
        if ($query == 1) {
            $this->_assert_equals($query, 1);
          
               return json_encode(array('status' => true, 'code' => 0, 'msg' => "Deactivated successfully"));
        }
        if ($query == 2) {
            $this->_assert_equals($query, 2);
              return json_encode(array('status' => false, 'code' => 1, 'msg' => "Invalid session key"));
            
        }
        if ($query == 3) {
            $this->_assert_equals($query, 3);
             return json_encode(array('status' => false, 'code' => 3, 'msg' => "Login first to Deactivated"));
           
        }
        if ($query == 4) {
            $this->_assert_equals($query, 4);
              return json_encode(array('status' => false, 'code' => 4, 'msg' => "Invalid Email Id or Password"));
            
        }
    }
 ///////////////////////////////////////testing for activate account//////////////////
    public function test_activate() {

        $activate = $this->activate;

        $result = "";
        $query1 = $this->account_model->activate_user($this->activate['invalidActivationCode']);
        $result = $result . "For First Input:<br/> Email Address : " . $this->activate['invalidActivationCode']['emailAddress'] . "<br/>activationCode:" . $this->activate['invalidActivationCode']['activationCode'] . "<br/> Result :" . $this->activate_result($query1) . "<br/><br/>";
        $query2 = $this->account_model->activate_user($this->activate['accountActivatedSuccessfully']);
        $result = $result . "For Second Input:<br/> Email Address : " . $this->activate['accountActivatedSuccessfully']['emailAddress'] . "<br/>activationCode:" . $this->activate['accountActivatedSuccessfully']['activationCode'] . "<br/> Result :" . $this->activate_result($query2) . "<br/><br/>";

        $query3 = $this->account_model->activate_user($this->activate['invalidemailId']);
        $result = $result . "For Third Input:<br/> Email Address : " . $this->activate['invalidemailId']['emailAddress'] . "<br/>activationCode:" . $this->activate['invalidemailId']['activationCode'] . "<br/> Result :" . $this->activate_result($query3) . "<br/><br/>";
        $query4 = $this->account_model->activate_user($this->activate['accountAlreadyActivated']);
        $result = $result . "For Fourth Input:<br/> Email Address : " . $this->activate['accountAlreadyActivated']['emailAddress'] . "<br/>activationCode:" . $this->activate['accountAlreadyActivated']['activationCode'] . "<br/> Result :" . $this->activate_result($query4) . "<br/><br/>";
        $this->message = $result;
    }

    function activate_result($query) {
        if ($query == 0) {
            $this->_assert_equals($query, 0);
            return json_encode(array('status' => false, 'code' => 5, 'msg' => "Invalid Activationcode"));
          
        }
        if ($query == 1) {
            $this->_assert_equals($query, 1);
            return json_encode(array('status' => true, 'code' => 0, 'msg' => "Activated successfully"));
           
        }
        if ($query == 2) {
            $this->_assert_equals($query, 2);
             return json_encode(array('status' => false, 'code' => 4, 'msg' => "Email address was not found in system"));
           
        }
        if ($query == 3) {
            $this->_assert_equals($query, 3);
            return 'Success: Fail, Account already activated';
        }
    }
    function test_userdetail()
    {
       $userdetail=  $this->userdetail;
        $result = "";
         $query1 = $this->account_model->retrieve_user_Details($this->userdetail['validuser']);
         $result = $result . "For First Input:<br/> Email Address : " . $this->userdetail['validuser']['userID'] . "<br/>activationCode:" . $this->userdetail['validuser']['sessionKey'] . "<br/> Result :" . $this->user_result($query1) . "<br/><br/>";
         $query2 = $this->account_model->retrieve_user_Details($this->userdetail['notvalid']);
         $result = $result . "For First Input:<br/> Email Address : " . $this->userdetail['notvalid']['userID'] . "<br/>activationCode:" . $this->userdetail['notvalid']['sessionKey'] . "<br/> Result :" . $this->user_result($query2) . "<br/><br/>";
         $this->message = $result;
         }
     function user_result($query) 
     {
         if (isset($query['sess_status']))
             return json_encode(array('status' => false, 'code' => 1, 'obj' => $query['sess_status']));
        else if (isset($query['status']))
             return json_encode(array('status' => false, 'code' => 3, 'obj' => $query['status']));
        else if ($query)
             return json_encode(array('status' => true, 'code' => 0, 'obj' => $query));

        else
             return json_encode(array('status' => false, 'code' => 1, 'msg' => "Server response error. Could not retrieve user details."));
     }

}

// End of file user_account_test.php */
